<?php

    require ( 'init.inc.php' );
    require ( 'checkadmin.inc.php');
    require ( __COMM_PATH . 'pagecontrol.inc.php' );
    require ( __CLASS_PATH . 'page_list.class.php' );
	
	// -------------------------------------------------------
    // 接收变量
    // -------------------------------------------------------
    $action = $_REQUEST["action"];
    $type = intval($_POST['timemode']);
	$num  = intval($_POST['timenum']);
    // -------------------------------------------------------
    // 批量操作
    // -------------------------------------------------------
   if ($action == "submit") {
	    $curdate = time();   //获得当前时间
        switch ($type) {
              case "1" :    
			              $schdate = $num == 0?(date("G") + 1) * 60 * 60:$num * 24 * 60 * 60;   //计算多少天前
						             
						  break;
            case "2" : 
						  $schdate = $num == 0?(date("w") + 1) * 24 * 60 * 60:$num * 7 * 24 * 60 *60;  //计算多少星期前
							     
						  break;
            case "3" : 
						  $schdate = $num == 0?date("j") * 24 * 60 * 60:$num * 30 * 24 * 60 * 60;  //计算多少月前
							  
						  break;
			case "4" :
						  $schdate = $num == 0?(date("z") + 1) * 24 * 60 * 60:$num * 365 * 24 * 60 * 60;  //计算多少年前
							
						  break;
				
        }
		   $tim= $curdate-$schdate;   //计算时间差
		   $result=$db->query("select `id`,`username`,`ordernum`,`orderprice`,`trueprice`,`mygif`,`ordertime` from `website_order` where '$tim' <= `ordertime` and  `ordertime` < '$curdate' and `status`=1");
		   $j=0;
		   $n=0;
		   while($res = $db->fetch_array($result)){
			      $dd[]=$res;
			   	 if(!empty($res['mygif'])){
	 					$gif_id = explode("@",$res['mygif']);
	 					for($i = 0;$i < count($gif_id);$i++){
		 			    	$g_result = $db->query("select `productsprice`,`products_name` from `website_products_daili` where `id`='$gif_id[$i]'");
				        	$g_row[$j][]  = $db->fetch_array($g_result);
		                }
			      }
				  $j++;
		   }
		   //print_r($g_row);
		   foreach($g_row as $value){
			   foreach($value as $sonval){
				   	   $pertotal+=$sonval['productsprice'];
				   }
				  $gtotal[]=$pertotal;
				  $pertotal=0;
			   }
		   //$rownum=$db->num_rows($result);
		 // $result=$db->query("select `id`,`username`,`lastload`,`loadnum` from `website_member` where '1248835751' <= `lastload` and  `lastload` < '1248842822'");
		  // print_r($result);
		  // $row=$db->fetch_array($result);
		  // print_r($row);
		  
	// -------------------------------------------------------
    // 生成销售报表
    // -------------------------------------------------------
		header("Content-Type: application/vnd.ms-excel");
		header("Accept-Ranges:bytes");
		header("Content-Disposition:attachment;filename=sales_".date('YmdHis').".xls"); //Execl文件名
		header("Pragma: no-cache");
		header("Expires: 0");
		echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"
		xmlns:x="urn:schemas-microsoft-com:office:excel"
		xmlns=" http://www.w3.org/TR/REC-html40">
		
		<meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
		<meta http-equiv=Content-Type content="text/html; charset=Utf-8">
		<!--[if gte mso 9]><xml>
		<x:ExcelWorkbook>
		<x:ExcelWorksheets>
		<x:ExcelWorksheet>
		<x:Name></x:Name>
		<x:WorksheetOptions>
		<x:DisplayGridlines/>
		</x:WorksheetOptions>
		</x:ExcelWorksheet>
		</x:ExcelWorksheets>
		</x:ExcelWorkbook>
		</xml><![endif]-->';
		 file_log($_SESSION['AdminUser'],"生成销售报表");
		echo "<table aling='left'><tr><td><strong>会员账号</strong></td><td><strong>订单号</strong></td><td></td><td><strong>定金</strong></td><td></td><td><strong>实际总额</strong></td><td></td><td><strong>礼物价格</strong></td><td><strong>订单时间</strong></td></tr>";
		foreach ($dd as $row){
			$ordertotal+=$row['orderprice'];
			$giftotal+=$gtotal[$n];
			$truetotal+=$row['trueprice'];
			echo "<tr align='left'><td>".$row['username']."</td><td>".$row['ordernum']."</td><td></td><td>".$row['orderprice']."</td><td></td><td>".$row['trueprice']."</td><td></td><td>".$gtotal[$n]."</td><td>".date("Y-m-d H:i:s",$row['ordertime'])."</td></tr>";
			$n++;
		}
		echo "<tr align='left'><td></td></td></td><td></td><td>订金总额</td><td>".$ordertotal."</td><td>行程總費用</td><td>".$truetotal."</td><td>礼物費用總額</td><td>".$giftotal."</td></tr>";
	    echo "</table>";
    }

?>